// pages/user/user.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 2,
    tabBar: [{
        "pagePath": "../index/index",
        "text": "首页",
        "iconPath": "/images/index.png",
        "selectedIconPath": "/images/index_h.png",
      },
      {
        "pagePath": "../fastCardApply/fastCardApply",
        "text": "快速开卡",
        "iconPath": "/images/card.png",
        "selectedIconPath": "/images/card_h.png",
      },
      {
        "pagePath": "../user/user",
        "text": "我的",
        "iconPath": "/images/user.png",
        "selectedIconPath": "/images/user_h.png",
      }
    ],

    rightIcon: '/images/icon-right.png',
    head: '/images/head.png',
    userInfo: [],
  },




  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton();
    this.getUserInfo();
    

  },

  //跳转到账号管理
  navAccountManage() {
    wx.navigateTo({
      url: '/package/pages/accountMannage/index',
    })
  },




  /**
   * 用户退出登录
   */
  outLogin: function () {
    // wx.removeStorageSync('token');
    // app.showToast('退出成功');
    // setTimeout(function () {
    //   wx.switchTab({
    //     url: '../index/index',
    //   })
    // }, 1000);

    var that = this;
    app.ajax({
      url: 'Salesman/login/log_out',
      method: "POST",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {

          //删除历史登录信息
          const loaginHistoryRoles = wx.getStorageSync('loginHistoryRoles') || [];
          if (loaginHistoryRoles.length > 0) {
            let index = loaginHistoryRoles.findIndex(item => item.id == that.data.userInfo.id && item.role == wx.getStorageSync('role'));
            if (~index) {
              loaginHistoryRoles.splice(index, 1);
            }
            wx.setStorageSync('loginHistoryRoles', loaginHistoryRoles);
          }


          wx.removeStorageSync('token');
          wx.removeStorageSync('openid');
          wx.removeStorageSync('role');
          wx.removeStorageSync('userInfo');
          app.showToast('退出成功', "success");
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/login/login',
            })
          }, 1000);
        } else {
          app.showToast(res.data.message)
        }

      }
    })

  },
  //去设置
  goSet(e) {
    console.log('是打电话', e.currentTarget.dataset.info);
    wx.navigateTo({
      url: '../installInfo/installInfo?set_type=' + e.currentTarget.dataset.set_type + '&&info=' + e.currentTarget.dataset.info,
    })

  },
  //选择照片方式
  chooseimage: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从手机相册选择', '拍照'],
      itemColor: "black",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.changePic('album')
          } else if (res.tapIndex == 1) {
            that.changePic('camera')
          }
        }
      }
    })

  },
  //修改头像
  changePic(type) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success(res) {
        console.info(res.tempFiles);
        // tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0];
        var url = app.globalData._url;

        var token = wx.getStorageSync('token');
        wx.uploadFile({
          url: url + 'Salesman/Login/editHead', //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          formData: {
            'user': 'test'
          },
          header: {
            'token': token
          },
          success(res) {
            var data = res.data;
            console.log(res);
            //do something
          }
        })
      }
    })
  },
  getUserInfo() {
    var that = this;
    app.ajax({
      url: 'Salesman/Login/getInfo',
      data: {},
      success: function (res) {

        if (res.data.code > 0) {
          that.setData({
            userInfo: res.data.data,
          })

        }
      },

    })
  },


  //切换自定义tabbar
  tabbarSwitch(e) {
    console.log(e);
    const {
      index,
      pagePath: path
    } = e.detail;
    const current = this.data.current;
    if (index == current) return;
    wx.redirectTo({
      url: path
    })
  }
})